package com.easyedu.teacher.service;

import com.easyedu.core.entity.po.Classroom;
import com.easyedu.core.exception.EasyEduException;
import com.easyedu.teacher.dao.ClassroomDao;
import com.easyedu.teacher.dao.ExamDao;
import com.easyedu.teacher.entity.dto.request.ReqCourseExam;
import com.easyedu.teacher.entity.query.CourseExamQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lmr
 **/
@Service
public class ExamService {

    private final ExamDao examDao;

    private final ClassroomDao classroomDao;

    public ExamService(ExamDao examDao, ClassroomDao classroomDao) {
        this.examDao = examDao;
        this.classroomDao = classroomDao;
    }

    public List<Classroom> getUnUserClassroom(int year, String semester, int week, int day, int lesson) {
        return classroomDao.getUnuseClassRoom(year, semester, week, day, lesson);
    }

    @Transactional(rollbackFor = Exception.class)
    public void setCourseExamTime(ReqCourseExam reqCourseExam) {
        CourseExamQuery courseExamQuery = new CourseExamQuery(reqCourseExam.getOpenCoursesId(),
                reqCourseExam.getYear(),
                reqCourseExam.getSemester(),
                reqCourseExam.getWeek(),
                reqCourseExam.getDay(),
                reqCourseExam.getLesson(),
                reqCourseExam.getExamStartTime(),
                reqCourseExam.getExamEndTime(),
                reqCourseExam.getClassroom());
        int i;
        try {
            i = examDao.setCourseExamTime(courseExamQuery);
        } catch (Exception e) {
            throw new EasyEduException(1999, "申请考试时间失败，请重试");
        }
        if (i != 1) {
            throw new EasyEduException(1999, "申请考试时间失败，请重试");
        }
    }
}
